import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)
import store from '@/store/index'
const routes = [
  
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children:[
      {
        path: '/users',
        name: 'users',
        component: () => import('../views/Users.vue')
      },
      {
        path: '/goods',
        name: 'goods',
        component: () => import('../components/goods/Goods.vue')
      },
      {
        path: '/params',
        name: 'params',
        component: () => import('../views/Params.vue')
       
      },
      {
        path: '/categories',
        name: 'categories',
        component: () => import('../components/Categories.vue')
       
      },
      {
        path: '/Goods/add',
        name: 'add',
        component: () => import('../components/goods/add.vue')
      },
      {
        path: '/orders',
        name: 'orders',
        component: () => import('../views/Orders.vue')
      },
      {
        path: '/rights',
        name: 'rights',
        component: () => import('../views/Rights.vue')
      },
      {
        path: '/reports',
        name: 'reports',
        component: () => import('../views/Reports.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../views/Roles.vue')
      }
    ]
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/Index.vue')
  },
  
  {
    path:'/',
    redirect:localStorage.getItem('RedirectPath')?localStorage.getItem('RedirectPath'):'/home'
  }
  
]

const router = new VueRouter({
  routes,
  mode:'hash'
})
router.beforeEach((to,from,next)=>{
  if(to.path=='/login'){
    next()
  }else{
    if(store.getters.token){
      next()
    }else{
      next('/login')
    }
  }
})
export default router
